

<template>
  <div class="app-container">
    <div class="title-flex">
      <div>单位列表</div>
      <div>
        <el-button-group class="ml-4">
          <el-button type="primary" @click="dialogFormVisible = true;getdanweileixingOptions()" > 新增</el-button>
          <el-button type="primary" @click="deleteList"> 删除</el-button>
          <el-button type="primary"  > 导入</el-button>
          <el-button type="primary"  @click="ExportBtn()"> 导出</el-button>
        </el-button-group>
      </div>
    </div>
    <div>
      <zzjgTable ref="getChildlist"></zzjgTable>
    </div>

  </div>
  <el-dialog v-model="dialogFormVisible" title="新增" width="70%">
    <div>
      <el-form
          ref="ruleFormRef"
          :model="ruleForm"
          :rules="rules"
          label-width="120px"
          class="demo-ruleForm"
          :size="formSize"
          status-icon
      >
        <div class="dialog-flex">
          <div>
            <el-form-item label="单位名称" prop="name">
              <el-input v-model="ruleForm.name" />
            </el-form-item>
            <el-form-item label="负责人" prop="agerName">
              <el-input v-model="ruleForm.agerName" />
            </el-form-item>
            <el-form-item label="教育部统计" prop="subjectClassId">
              <el-radio-group v-model="ruleForm.subjectClassId" @change="GetsubjectClassId">
                <el-radio label="科技类" value="2"/>
                <el-radio label="社科类" value="1"/>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="一级学科" prop="subjectId" >
              <el-select
                  v-model="ruleForm.subjectId"
                  placeholder="请选择一级学科"
                  @focus="getyijixuekeOptions"
              >
                <el-option
                    v-for="item in yijixuekeOptions"
                    :key="item.id"
                    :label="item.label"
                    :value="item.id"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="是否统计" prop="isState">
              <el-radio-group v-model="ruleForm.isState">
                <el-radio label="统计" value="1"/>
                <el-radio label="不统计" value="0"/>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="批准机构" prop="approve">
              <el-select
                  v-model="ruleForm.approve"
                  placeholder="请选择批准机构"
              >
                <el-option key="学校主管部门批准" label="学校主管部门批准" value="学校主管部门批准"/>
                <el-option key="非学校主管部门批准" label="非学校主管部门批准" value="非学校主管部门批准"/>
                <el-option key="学校自建" label="学校自建" value="学校自建"/>
              </el-select>
            </el-form-item>

          </div>
          <div>
            <el-form-item label="联系人">
              <el-input v-model="ruleForm.linkMan" />
            </el-form-item>
            <el-form-item label="成立时间">
              <el-date-picker
                  v-model="ruleForm.unitCreateDate"
                  type="date"
                  format="YYYY/MM/DD"
                  value-format="YYYY-MM-DD"
                  aria-label="选择时间"
                  placeholder="选择时间"
              />
            </el-form-item>
            <el-form-item label="单位编号">
              <el-input v-model="ruleForm.code" autocomplete="off" />
            </el-form-item>
            <el-form-item label="单位类型">
              <el-radio-group v-model="ruleForm.unitTypeId">
                <el-radio
                    v-for="item in danweileixingOptions"
                    :key="item.name"
                    :label="item.name"
                    :value="item.code"
                />
              </el-radio-group>
            </el-form-item>
            <el-form-item label="联系电话" >
              <el-input v-model="ruleForm.tel" autocomplete="off" />
            </el-form-item>
            <el-form-item label="排序号"  prop="orderId">
              <el-input v-model.number="ruleForm.orderId" type="text" />
            </el-form-item>
          </div>
        </div>
        <div>
          <el-form-item label="简介" >
            <el-input v-model="ruleForm.intro" type="textarea" />
          </el-form-item>
          <el-form-item label="备注" >
            <el-input v-model="ruleForm.standBY1" type="textarea" />
          </el-form-item>
        </div>
        <div style="margin-left:35%">
          <el-form-item>
            <el-button style="text-align: center" type="primary" @click="submitForm(ruleFormRef)">
              保存
            </el-button>
            <el-button style="text-align: center" @click="resetForm(ruleFormRef,'closeone')">关闭</el-button>
          </el-form-item>
        </div>

      </el-form>
    </div>
  </el-dialog>
</template>
<script lang="ts" setup>
import {getCategory} from "@/api/researcherNavApi/researcherNavApi"
import {DrawerProps, ElMessage, ElMessageBox} from "element-plus";
import zzjgTable from './zzjgTable.vue'; //表格组件
import {
  insertOrganizationList
} from '@/api/researcherNavApi/zzjgApi'
import {ref} from "vue";

//导出
const ExportBtn = function (){
  getChildlist.value.Export()
}
const  GetsubjectClassId = function (){
    ruleForm.subjectId = ''
}

//一级学科
const yijixuekeOptions = ref([])
const getyijixuekeOptions =function () {
  getCategory(
      {
        'bizName':'一级学科',
        valueCascade: ruleForm.subjectClassId
      }
  )
      .then((res)=>{

        yijixuekeOptions.value = res.data
      })
}

//批量删除
const deleteList= function (){
  getChildlist.value.open()
}
//批量删除end
//单位类型
const danweileixingOptions = ref()
const getdanweileixingOptions =function () {
  getCategory(
      {
        'bizName':'单位类型'
      }
  )
      .then((res)=>{

        danweileixingOptions.value = res.data
      })
}
// 编辑
const dialogFormVisible = ref(false)
const formLabelWidth = '140px'
const form = reactive({
  name: '',
  region: '',
  type: [],
  resource: '',
  desc: '',
})
const formSize = ref('default')
const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive<RuleForm>({
  name:'', //单位名称
  agerName:'', //负责人
  linkMan:'', //联系人
  unitCreateDate:'',  //;成立时间
  subjectClassId:'2',  //;教育部统计
  subjectId:'',  //;一级学科
  isState:'',  //;是否统计
  approve:'',  //;批准机构

  code:'',   //;单位编号
  unitTypeId:'', //单位类型
  tel:'', //联系电话
  orderId:'',  //; 排序号

  intro:'', //;简介
  standBY1:'', //;备注

})

interface RuleForm {
  name: string
  agerName: string
  linkMan: string
  subjectClassId: string
  subjectId: string
  isState: string
  approve: string
  unitCreateDate: string
  code: string
  unitTypeId: string
  tel: string
  orderId: string
  intro: string
  standBY1: string

}
const checkAge = (rule: any, value: any, callback: any) => {
  if (value === '' || value === null || value === undefined) {
    // 如果值为空，直接通过验证
    callback();
  } else if (!Number.isInteger(Number(value))) {
    // 如果值不为空且不是整数，验证失败
    callback(new Error('请输入数字'));
  } else {
    // 如果值不为空且是整数，通过验证
    callback();
  }
  // if (!value) {
  //   return callback(new Error('请输入数字,不可为0'))
  // }
  // setTimeout(() => {
  //   if (!Number.isInteger(value)) {
  //     callback(new Error('请输入数字,不可为0'))
  //   } else {
  //       callback()
  //   }
  // }, 1000)
}
const rules = reactive<FormRules<RuleForm>>({
  name: [
    { required: true, message: '请输入单位名称', trigger: 'blur' },
  ],
  agerName: [
    { required: true, message: '请输入负责人', trigger: 'blur' },
  ],
  subjectId: [
    {required: true, message: '请选择科学一级学科', trigger: 'change',},
  ],
  subjectClassId:[
    {required: true, message: '请选择统计', trigger: 'change',},
  ],
  isState:[
    {required: true, message: '请选择是否统计', trigger: 'change',},
  ],
  approve: [
    { required: true, message: '请输入批准机构', trigger: 'blur' },
  ],
  orderId: [{ validator: checkAge, trigger: 'blur' }],
})
const getChildlist = ref<any>()
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      insertOrganizationList(ruleForm)
          .then((res)=>{
            if(res.code ==200){
              ElMessage({
                type: 'success',
                message: res.data,
              })
              getChildlist.value.fathergeilist()
              dialogFormVisible.value = false
            }else {
              ElMessage({
                type: 'info',
                message: res.msg,
              })
            }
          })

      console.log('submit!')
    } else {
      console.log('error submit!', fields)
    }
  })
}

const resetForm = (formEl: FormInstance | undefined , closeone) => {
  if (!formEl) return
  formEl.resetFields()
  if( closeone &&  closeone =='closeone'){
    dialogFormVisible.value = false
  }
}
// 编辑

</script>
<style scoped lang="scss">
.title-flex{
  display: flex;
  justify-content: space-between;
  text-align: center;

}
.dialog-flex{
  display: flex;
}
.dialog-flex > div{
  width: 50%;
}
</style>